﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Sharkbite.Irc;

namespace CSharpIrcBot.Templates
{
    public partial class ChannelTemplate : Form
    {
        public ChannelTemplate(string Channel)
        {
            InitializeComponent();
            this.Text = Channel;
        }

        private void ChannelTemplate_Load( object sender, EventArgs e )
        {
            Global.ircConnection.Listener.OnPublic += new Sharkbite.Irc.PublicMessageEventHandler( Listener_OnPublic );
        }
        
        void Listener_OnPublic( Sharkbite.Irc.UserInfo user, string channel, string message )
        {
            if( channel == this.Text )
            {
                FormatRTB( rtbChannel, string.Format("[{0}] <{1}> {2}", DateTime.Now.ToString("hh:mm"), user.Nick, message) );
            }
        }

        private static void FormatRTB( RichTextBox rtb, string text )
        {
            if( rtb.InvokeRequired )
            {
                rtb.BeginInvoke( new UpdateTextCallback( FormatRTB ), new object[] { rtb, text } );
            }
            else
            {
                Font f = rtb.Font;
                Color cText = rtb.ForeColor;
                Color cBack = rtb.BackColor;
                for( int i = 0; i < text.Length; i++ )
                {
                    switch( char.Parse( text.Substring( i, 1 ) ) )
                    {
                        case (char) 3:
                            cText = CheckColor( text, ref i );
                            if( i + 1 < text.Length )
                            {
                                if( text.Substring( i + 1, 1 ) == "," )
                                {
                                    i += 1;
                                    cBack = CheckColor( text, ref i );
                                }
                            }
                            break;
                        case (char) 2:
                            f = ( f.Bold == true ? new Font( f, FontStyle.Regular ) : new Font( f, f.Style | FontStyle.Bold ) );
                            break;
                        case (char) 31:
                            f = ( f.Underline == true ? new Font( f, FontStyle.Regular ) : new Font( f, f.Style | FontStyle.Underline ) );
                            break;
                        default:
                            rtb.SelectionStart = rtb.Text.Length;
                            rtb.SelectionLength = 1;
                            rtb.SelectionFont = f;
                            rtb.SelectionColor = cText;
                            rtb.SelectionBackColor = cBack;
                            rtb.SelectedText = text.Substring( i, 1 );
                            break;

                    }
                }
                rtb.AppendText( Environment.NewLine );
            }
        }
        private static Color CheckColor( string text, ref int i )
        {
            Color color = Color.Black;
            if( i + 3 < text.Length )
            {
                if( text.Substring( i + 1, 2 ) == "15" )
                {
                    color = Color.FromName( ( (MircColor) 15 ).ToString() );
                    i += 2;
                }
                if( text.Substring( i + 1, 2 ) == "14" )
                {
                    color = Color.FromName( ( (MircColor) 14 ).ToString() );
                    i += 2;
                }
                if( text.Substring( i + 1, 2 ) == "13" )
                {
                    color = Color.FromName( ( (MircColor) 13 ).ToString() );
                    i += 2;
                }
                if( text.Substring( i + 1, 2 ) == "12" )
                {
                    color = Color.FromName( ( (MircColor) 12 ).ToString() );
                    i += 2;
                }
                if( text.Substring( i + 1, 2 ) == "11" )
                {
                    color = Color.FromName( ( (MircColor) 11 ).ToString() );
                    i += 2;
                }
                if( text.Substring( i + 1, 2 ) == "10" )
                {
                    color = Color.FromName( ( (MircColor) 10 ).ToString() );
                    i += 2;
                }
            }
            if( i + 2 < text.Length )
            {
                if( text.Substring( i + 1, 1 ) == "0" )
                {
                    color = Color.FromName( ( (MircColor) 0 ).ToString() );
                    i += 1;
                }
                if( text.Substring( i + 1, 1 ) == "1" )
                {
                    color = Color.FromName( ( (MircColor) 1 ).ToString() );
                    i += 1;
                }
                if( text.Substring( i + 1, 1 ) == "2" )
                {
                    color = Color.FromName( ( (MircColor) 2 ).ToString() );
                    i += 1;
                }
                if( text.Substring( i + 1, 1 ) == "3" )
                {
                    color = Color.FromName( ( (MircColor) 3 ).ToString() );
                    i += 1;
                }
                if( text.Substring( i + 1, 1 ) == "4" )
                {
                    color = Color.FromName( ( (MircColor) 4 ).ToString() );
                    i += 1;
                }
                if( text.Substring( i + 1, 1 ) == "5" )
                {
                    color = Color.FromName( ( (MircColor) 5 ).ToString() );
                    i += 1;
                }
                if( text.Substring( i + 1, 1 ) == "6" )
                {
                    color = Color.FromName( ( (MircColor) 6 ).ToString() );
                    i += 1;
                }
                if( text.Substring( i + 1, 1 ) == "7" )
                {
                    color = Color.FromName( ( (MircColor) 7 ).ToString() );
                    i += 1;
                }
                if( text.Substring( i + 1, 1 ) == "8" )
                {
                    color = Color.FromName( ( (MircColor) 8 ).ToString() );
                    i += 1;
                }
                if( text.Substring( i + 1, 1 ) == "9" )
                {
                    color = Color.FromName( ( (MircColor) 9 ).ToString() );
                    i += 1;
                }
            }
            return color;
        }
        delegate void UpdateTextCallback( RichTextBox rtb, string text );
 
    }
}
